<?php
//------------------------
// 前台登陆控制器
//-------------------------

namespace app\home\controller;

use app\common\model\User as UserModel;
use app\common\model\UserDetails as UserDetailsModel;
use app\common\model\UserRank as UserRankModel;
use \think\Request;
use think\Config;
use think\Controller;
use think\Db;
use think\Session;


class Login extends Controller
{
    protected $UserModel;
    protected $UserDetailsModel;
	protected $UserRankModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->UserModel        = new UserModel();
        $this->UserDetailsModel = new UserDetailsModel();
        $this->UserRankModel    = new UserRankModel();
    }


	// 登陆页面
    public function index(){

        return $this->fetch();
    }

    // 登陆处理
    public function loginProc(){
        $data            = $this->request->param();

        $validate_result = $this->validate($data, 'HomeLogin');

        if ($validate_result !== true) {
                $this->error($validate_result);
        } else {
                $where['username'] = $data['username'];
                $where['password'] = user_encryption($data['password']);

                $user = Db('user')->field('id,username,email_code')->where($where)->find();

                if (!empty($user)) {
                    if ($user['email_code'] != '') {
                        $this->error('该帐号还没通过邮件验证');
                    } else {
                        Session::set('user_id', $user['id']);
                        Session::set('user_name', $user['username']);
                        // 存入用户基本信息
                        $userInof = $this->UserDetailsModel->where('user_id',$user['id'])->find()->toArray();
                        Session::set('user_info', $userInof);
                        // 存入会员级别，会员折购率
                        $rankInfo = $this->UserRankModel->where('min_exp','ELT',$userInof['exp'])->order('min_exp desc')->find()->toArray();
                        Session::set('rank_info', $rankInfo);

                        Db('user')->update(
                            [
                                'last_login_time' => time(),
                                'last_login_ip'   => $this->request->ip(),
                                'id'              => $user['id']
                            ]
                        );
                        Db('user')->where('id', $user['id'])->setInc('login_count');

                        // 将cookie中的数据传到数据库
                        model('Cart')->dataCookieToDb();

                        // 跳回页面
                        if (Session::has('returnUrl')) {
                            $returnUrl = Session::get('returnUrl');
                            Session::delete('returnUrl');
                        } else {
                            $returnUrl = 'home/index/index';
                        }

                        $this->success('登录成功', $returnUrl);
                    }
                } else {
                    $this->error('用户名或密码错误');
                }
            }
    }

    /**
     * 退出登录
     */
    public function logout(){
        Session::delete('user_id');
        Session::delete('user_name');
        $this->success('退出成功,欢迎使用SF商城，期待你的再次光临', 'home/login/index');
    }

    // 登陆跳转前保存原页面的地址
    public function saveAndLogin(){
        // 获取AJAX是从哪个页面发过来的
        Session::set('returnUrl', $_SERVER['HTTP_REFERER']);
        $this->success();
    }






}
